<script setup lang='ts'>
import type { PlusColumn } from 'plus-pro-components'

import { useAddSupplierInfo } from '@fl/api/man-api'
import { SubmitBtn } from '@fl/components'

const { isPending, mutate } = useAddSupplierInfo()

const visible = defineModel<boolean>('visible', { required: true })

const formRef = ref()

const state = ref({
    creditCode: '',
    name: '',
})

const columns: PlusColumn[] = [
    {
        label: '公司名称',
        prop: 'name',
    },
    {
        label: '社会信用代码',
        prop: 'creditCode',
    },
]

const rules = {
    creditCode: [
        {
            message: '请输入社会信用代码',
            required: true,
        },
    ],
    name: [
        {
            message: '请输入公司名称',
            required: true,
        },
    ],
}

async function submit() {
    const valid = await formRef.value.formInstance.validate()
    if (valid) {
        mutate({ ...state.value }, {
            onSuccess: () => {
                ElMessage.success('操作成功')
                cancel()
            },
        })
    }
}

function cancel() {
    visible.value = false
}

function reset() {
    formRef.value.formInstance.resetFields()
}
</script>

<template>
    <PlusDialog v-model="visible"
                title="新增企业"
                width="800"
                close-on-press-escape
                @cancel="cancel"
                @closed="reset"
    >
        <PlusForm ref="formRef"
                  v-model="state"
                  :columns="columns"
                  :label-width="130"
                  :rules="rules"
                  :has-footer="false"
        />

        <template #footer>
            <div class="mx-auto">
                <el-button @click="cancel">
                    取消
                </el-button>

                <SubmitBtn type="primary"
                           :loading="isPending"
                           @click="submit"
                >
                    提交
                </SubmitBtn>
            </div>
        </template>
    </PlusDialog>
</template>
